//
//  FaceView.m
//  weibo-01
//
//  Created by HZApple on 16/5/20.
//  Copyright © 2016年 hdu. All rights reserved.
//

#import "FaceView.h"
#import "UIViewExt.h"

#define kItemSize 45
#define kFaceImageSize 30

#define kLinesPerPage 4

@interface FaceView ()

//@property(nonatomic, strong)UIImageView *selectImgView;

@end

@implementation FaceView


//- (UIImageView *)selectImgView {
//    
//    if (_selectImgView == nil) {
//        _selectImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
//        
//        
//        _selectImgView.image = [UIImage imageNamed:@"emoticon_keyboard_magnifier.png"];
//        
//        //宽高自适应
//        [_selectImgView sizeToFit];
//        
//        [self addSubview:_selectImgView];
//        
//        UIImageView *faceImgView = [[UIImageView alloc] initWithFrame:CGRectMake((_selectImgView.width - kFaceImageSize)/2, 15, kFaceImageSize, kFaceImageSize)];
//        
//        faceImgView.tag = 100;
//        
//        
//        [_selectImgView addSubview:faceImgView];
//        
//    }
//    
//    return _selectImgView;
//    
//    
//}

- (instancetype)init {
    if (self = [super init]) {
        [self _createData];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _createData];
        
    }
    return self;
}


- (void)_createData {
    
  
    _items2D = [NSMutableArray array];
    
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"emoticons" ofType:@"plist"];
    NSArray *emoticons = [NSArray arrayWithContentsOfFile:filePath];
    
    
    NSMutableArray *item1D = nil;
    
    NSInteger columnsPerLine =  kScreenWidth / kItemSize;
    
    NSUInteger count = kLinesPerPage * columnsPerLine;
    
    
    
    
    for (NSInteger i = 0; i < emoticons.count; i++) {
        
        if (item1D == nil || item1D.count == count) {
            
            item1D = [NSMutableArray arrayWithCapacity:count];
  
            [_items2D addObject:item1D];
            
          }

        [item1D addObject:emoticons[i]];
   
        }
    
    
    CGRect frame = self.frame;
    
    frame.size.width = _items2D.count * kScreenWidth;
    frame.size.height = kItemSize * kLinesPerPage;
    
    self.frame = frame;
    //NSLog(@"%@",NSStringFromCGRect(self.frame));
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    NSInteger row = 0;
    NSInteger column = 0;
    
    NSInteger columnsPerLine =  kScreenWidth / kItemSize; //列数
    
    for (NSInteger i = 0; i < _items2D.count; i++) {
        
        NSArray *item1D = _items2D[i];
        
        for (NSInteger j = 0; j < item1D.count; j++) {
            
            NSDictionary *item = item1D[j];
            NSString *imgName = item[@"png"];
            UIImage *image = [UIImage imageNamed:imgName];
            
            CGFloat x = column * kItemSize + (kItemSize - kFaceImageSize)/2 + i * kScreenWidth;
            
            CGFloat y = row * kItemSize + (kItemSize - kFaceImageSize)/2;
            
            [image drawInRect:CGRectMake(x, y, kFaceImageSize, kFaceImageSize)];
            
            
            column++;
            if (column % columnsPerLine == 0) {
                column = 0;
                row++;
            }
            if (row == kLinesPerPage) {
                row = 0;
            }
            
        }
        
    }
    

}




- (void)touchFace:(CGPoint)point {
    
    
    NSInteger page = point.x / kScreenWidth;
    
    if (page >= _items2D.count || page < 0) {
        
        return;
        
    }
    
    //计算当前表情的行和列
    NSInteger column = (point.x - ((kItemSize - kFaceImageSize)/2 + page * kScreenWidth))/kItemSize;
    
    NSInteger row = (point.y - (kItemSize - kFaceImageSize)/2)/kItemSize;
    //NSLog(@"row:%li, column:%li", row, column);
        //容错处理 7列 0-6
    NSInteger columnsPerLine =  kScreenWidth / kItemSize; //每行有多少列
    if (column > columnsPerLine - 1) {
        column = columnsPerLine - 1;
    }
    
    if (column < 0) {
        column = 0;
    }
    
    if (row > kLinesPerPage - 1) {
        row = kLinesPerPage - 1;
    }
    
    if (row < 0) {
        row = 0;
    }
    

    NSInteger index = row * columnsPerLine + column;

    NSArray *item1D = _items2D[page];
    
    if (index >= item1D.count || index < 0) {
        _faceInfo = nil;
        return ;
    }
    
    _faceInfo = item1D[index];
    
//    NSString *imageName = _faceInfo[@"png"];
//    NSString *faceName = _faceInfo[@"chs"];
    
    
    //NSLog(@"%@:%@",faceName,imageName);
    

    //UIImageView *faceImageView = (UIImageView *)[self.selectImgView viewWithTag:100];
    //faceImageView.image = [UIImage imageNamed:imageName];
    

//    CGFloat x = column * kItemSize + kItemSize/2 + page * kScreenWidth;
    CGFloat x = column * kItemSize + kItemSize/2;
    CGFloat y = row * kItemSize + kItemSize/2;
    
    _viewCenter = CGPointMake(x, 0);
    _viewBottom = y;
   // NSLog(@"x=%f,y=%f",x,y);
    
 
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //_selectImgView.hidden = NO;
    _viewHidden = NO;
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)self.superview;
        scrollView.scrollEnabled = NO;
    }
    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    [self touchFace:point];
    [[NSNotificationCenter defaultCenter] postNotificationName:kFaceViewChange object:nil];
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    [self touchFace:point];
    [[NSNotificationCenter defaultCenter] postNotificationName:kFaceViewChange object:nil];
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    //_selectImgView.hidden = YES;
    _viewHidden = YES;
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scrollView = (UIScrollView *)self.superview;
        scrollView.scrollEnabled = YES;
        
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:kFaceViewEnd object:nil];
    
}


@end
